import 'package:flutter/material.dart';

class FocusOnTextFeildDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Focus On TextFeild Demo'),
      ),
      body: ListView(
        children: <Widget>[
          TextField(
            // Focus a text field as soon as it’s visible
            autofocus: true,
          ),
          // Focus a text field when a button is tapped
          MyCustemForm(),
        ],
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.edit),
      ),
    );
  }
}

class MyCustemForm extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _MyCustemFormState();
  }
}

class _MyCustemFormState extends State<MyCustemForm> {
  FocusNode myFocusNode;
  @override
  void initState() {
    super.initState();
    myFocusNode = FocusNode();
  }

  @override
  void dispose() {
    myFocusNode.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        TextField(
          focusNode: myFocusNode,
          decoration: InputDecoration(
            hintText: 'ss',
          ),
        ),
        FlatButton(
          child: Text('点击'),
          onPressed: () => FocusScope.of(context).requestFocus(myFocusNode),
        ),
      ],
    );
  }
}
